package org.rcy.framework.security.authentication;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.rcy.framework.api.cacheable.CacheAble;
import org.rcy.framework.api.cacheable.CacheKeyStrategy;
import org.rcy.framework.api.security.UserPrincipal;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;

import java.security.Principal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class AuthenticationToken implements Authentication, CacheAble, StatelessAuthenticationToken {
    private static final long serialVersionUID = -243193403693546680L;
    public static final String CACHE_GROUP = "token";
    public static final String NAMESPACE = "passport";
    private final UserPrincipal principal;
    private final transient Collection<GrantedAuthority> authorities;
    private List<String> simpleAuthorities;
    private boolean authenticated;
    private transient Object details;
    private String statelessToken;
    private transient Credentials credentials;
    private final UserPrivilegeType userPrivilege;
    private String csrfToken;

    public AuthenticationToken(UserPrincipal principal, Collection<? extends GrantedAuthority> authorities, UserPrivilegeType userPrivilege) {
        this.authenticated = false;
        if (principal != null && authorities != null) {
            Iterator var4 = authorities.iterator();

            GrantedAuthority a;
            do {
                if (!var4.hasNext()) {
                    ArrayList<GrantedAuthority> temp = new ArrayList(authorities.size());
                    temp.addAll(authorities);
                    this.authorities = Collections.unmodifiableList(temp);
                    this.principal = principal;
                    this.authenticated = true;
                    this.userPrivilege = userPrivilege;
                    return;
                }

                a = (GrantedAuthority)var4.next();
            } while(a != null);

            throw new IllegalArgumentException("Authorities collection cannot contain any null elements");
        } else {
            throw new IllegalArgumentException("Cannot pass null or empty values to constructor");
        }
    }

    public AuthenticationToken(UserPrincipal principal, CredentialsToken credentials, Collection<? extends GrantedAuthority> authorities) {
        this(principal, credentials, authorities, (UserPrivilegeType)null);
    }

    public AuthenticationToken(UserPrincipal principal, CredentialsToken credentials, Collection<? extends GrantedAuthority> authorities, UserPrivilegeType userPrivilege) {
        this.authenticated = false;
        if (principal != null && credentials != null && authorities != null) {
            Iterator var5 = authorities.iterator();

            GrantedAuthority a;
            do {
                if (!var5.hasNext()) {
                    ArrayList<GrantedAuthority> temp = new ArrayList(authorities.size());
                    temp.addAll(authorities);
                    this.authorities = Collections.unmodifiableList(temp);
                    this.principal = principal;
                    this.credentials = credentials;
                    this.details = credentials.getDetails();
                    this.authenticated = true;
                    this.simpleAuthorities = this.changeToSimple(authorities);
                    this.userPrivilege = userPrivilege;
                    return;
                }

                a = (GrantedAuthority)var5.next();
            } while(a != null);

            throw new IllegalArgumentException("Authorities collection cannot contain any null elements");
        } else {
            throw new IllegalArgumentException("Cannot pass null or empty values to constructor");
        }
    }

    public Credentials getCredentials() {
        return this.credentials;
    }

    public UserPrincipal getPrincipal() {
        return this.principal;
    }

    public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
        if (isAuthenticated) {
            throw new IllegalArgumentException("Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");
        } else {
            this.authenticated = false;
        }
    }

    public String getStatelessToken() {
        return this.statelessToken;
    }

    public void setStatelessToken(String statelessToken) {
        this.statelessToken = statelessToken;
    }

    public String getCsrfToken() {
        return this.csrfToken;
    }

    public void setCsrfToken(String csrfToken) {
        this.csrfToken = csrfToken;
    }

    @Override
    public String getNameSpace() {
        return "passport";
    }

    @JsonIgnore
    public String getCacheKey() {
        return getCacheKey(this.statelessToken);
    }

    public static String getCacheKey(String token) {
        return CacheKeyStrategy.getCacheKey(token, new String[]{"passport", "token"});
    }

    public String getNamespace() {
        return "passport";
    }

    public String getName() {
        if (this.getPrincipal() instanceof Principal) {
            return this.getPrincipal().getName();
        } else {
            return this.getPrincipal() == null ? "" : this.getPrincipal().toString();
        }
    }

    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    public Object getDetails() {
        return this.details;
    }

    public boolean isAuthenticated() {
        return this.authenticated;
    }

    public List<String> simpleAuthorities() {
        return this.simpleAuthorities;
    }

    public UserPrivilegeType getUserPrivilege() {
        return this.userPrivilege;
    }

    private List<String> changeToSimple(Collection<? extends GrantedAuthority> authorities) {
        List<String> list = new ArrayList();
        Iterator var3 = authorities.iterator();

        while(var3.hasNext()) {
            GrantedAuthority authority = (GrantedAuthority)var3.next();
            list.add(authority.getAuthority());
        }

        return list;
    }

    public int hashCode() {
        int result = 1;
        result = 31 * result + (this.authenticated ? 1231 : 1237);
        result = 31 * result + (this.authorities == null ? 0 : this.authorities.hashCode());
        result = 31 * result + (this.principal == null ? 0 : this.principal.hashCode());
        return result;
    }

    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        } else if (obj == null) {
            return false;
        } else if (this.getClass() != obj.getClass()) {
            return false;
        } else {
            AuthenticationToken other = (AuthenticationToken)obj;
            if (this.authenticated != other.authenticated) {
                return false;
            } else {
                if (this.authorities == null) {
                    if (other.authorities != null) {
                        return false;
                    }
                } else if (!this.authorities.equals(other.authorities)) {
                    return false;
                }

                if (this.principal == null) {
                    if (other.principal != null) {
                        return false;
                    }
                } else if (!this.principal.equals(other.principal)) {
                    return false;
                }

                return true;
            }
        }
    }
}
